package action.ManagerAction;

import com.opensymphony.xwork2.ActionSupport;
import entity.CroMenus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroMenusService;
import vo.ShowMenuVo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuweijie on 2017/4/28.
 */
public class MenuAction extends ActionSupport {

    private List<ShowMenuVo> showMenuVos;
    private String menuid;
    private String parentId;
    private String menus[];
    private String menusurl[];
    private String role;
    private String successResult;


    @Autowired
    private CroMenusService croMenusService;

    /*
    * 展示全部菜单
    * */
    @RequiresPermissions("user:select")
    public String listMenu() {
        showMenuVos = croMenusService.SortMenu();
        return SUCCESS;
    }

    /*
    * 批量添加菜单
    * */
    @RequiresPermissions("user:write")
    public String addMenu() {
        List<CroMenus> croMenusList = new ArrayList<>();
        for (int i = 0; i < menus.length; i++) {
            CroMenus croMenus = new CroMenus();
            croMenus.setMenuname(menus[i]);
            croMenus.setParentId(parentId);
            croMenus.setUrl(menusurl[i]);
            System.out.println(menus[i]);
            System.out.println(menusurl[i]);
            croMenusList.add(croMenus);
        }
        croMenusService.addMenus(croMenusList);
        return SUCCESS;
    }

    /**
     * 更新菜单
     * */
    @RequiresPermissions("user:update")
    public String updateMenu(){
        CroMenus croMenus = croMenusService.FindMenusById(menuid);
        croMenus.setMenuname(menus[0]);
        croMenus.setUrl(menusurl[0]);
        if(croMenusService.updateMenu(croMenus)){
            successResult = "更新成功！";
        }else {
            successResult = "更新失败！";
        }
        return SUCCESS;
    }

    /**
     * 删除菜单
     * */
    @RequiresPermissions("user:delete")
    public String delMenu(){
        if (croMenusService.delMenuById(menuid)){
            successResult = "删除成功！";
        }else{
            successResult = "删除失败！";
        }
        return SUCCESS;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String[] getMenus() {
        return menus;
    }

    public void setMenus(String[] menus) {
        this.menus = menus;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<ShowMenuVo> getShowMenuVos() {
        return showMenuVos;
    }

    public void setShowMenuVos(List<ShowMenuVo> showMenuVos) {
        this.showMenuVos = showMenuVos;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String[] getMenusurl() {
        return menusurl;
    }

    public void setMenusurl(String[] menusurl) {
        this.menusurl = menusurl;
    }

    public String getMenuid() {
        return menuid;
    }

    public void setMenuid(String menuid) {
        this.menuid = menuid;
    }
}
